package com.bwton.msx.ccb.ui;

import androidx.annotation.StringRes;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import android.view.WindowManager;

import com.bwton.msx.ccb.widget.dialog.ProgressDialogFragment;


public class BaseActivity extends FragmentActivity {

    protected ProgressDialogFragment mProgressDialogFragment;

    @Override
    protected void onResume() {
        super.onResume();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    @Override
    protected void onPause() {
        super.onPause();
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    protected void showProgress() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mProgressDialogFragment != null && mProgressDialogFragment.isAdded()) {
                    mProgressDialogFragment.dismiss();
                }
                mProgressDialogFragment = ProgressDialogFragment.newInstance();
                final FragmentManager fragmentManager = getSupportFragmentManager();
                mProgressDialogFragment.show(fragmentManager, "ProgressDialog");

            }
        });
    }

    protected void showProgress(@StringRes int messageResID) {
        showProgress(getString(messageResID));
    }

    protected void showProgress(final String message) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mProgressDialogFragment != null && mProgressDialogFragment.isAdded()) {
                    mProgressDialogFragment.dismiss();
                }
                mProgressDialogFragment = ProgressDialogFragment.newInstance(message);
                mProgressDialogFragment.show(getSupportFragmentManager(), "ProgressDialog");
            }
        });
    }

    protected void dismissProgress() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mProgressDialogFragment.dismiss();
            }
        });
    }
}
